package NBJL;

import javagprssrv.StructClass.structIATask;
import javagprssrv.StructClass.structTCPServerSocket;


public class TrdTCPDataTrans_NBJL extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTerm term;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String AsynTelNo;
    java.lang.String TaskID;
    java.lang.String ZB;
    java.lang.String TelNO;
    java.lang.String YBH;
    java.lang.String WYM;
    java.lang.String TermNO;
    
    public TrdTCPDataTrans_NBJL(final javagprssrv.StructClass.structRecvData _recvData) {
        super("TrdTCPDataTrans_NBJL");
        this.term = new javagprssrv.StructClass.structTerm();
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.recvData = _recvData;
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
    }
    
    @java.lang.Override
    public void run() {
        try {
            java.lang.Thread.sleep(500L);
            final int Data_0 = this._Data.charAt(0);
            if (Data_0 == 36) {
                this.WYM = this._Data.substring(1, 9);
                this.do_NBJL_denglu();
            }
            if (this._Data.charAt(0) == 'Y') {
                this.do_59();
            }
            if (this._Data.charAt(0) == '\u0006') {
                this.locateTask();
                boolean isJSSJ = false;
                try {
                    isJSSJ = this.task.Task_isJSSJ;
                }
                catch (java.lang.Exception ex) {}
                if (isJSSJ) {
                    synchronized (javagprssrv.JGS_Param.IATasks) {
                        for (int idx = (javagprssrv.JGS_Param.IATasks.size()) - 1; idx >= 0; --idx) {
                            if (((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.task.JSSJ_ClientIP) && ((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.task.JSSJ_ClientPort) {
                            	((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).RecvData = this._Data;
                                break;
                            }
                        }
                    }
                }
                else {
                    synchronized (javagprssrv.JGS_Param.UnSavedRecvData_TCP) {
                        this.recvData.RecvDataType = 6;
                        javagprssrv.JGS_Param.UnSavedRecvData_TCP.add(this.recvData);
                    }
                }
                final int cmd = this._Data.substring(4, 5).charAt(0);
                if (cmd == 243) {
                    return;
                }
                if (cmd == 244) {
                    return;
                }
                if (cmd == 245) {
                    return;
                }
                this.do_41();
            }
        }
        catch (java.lang.Exception ex2) {}
    }
    //数据库不符
    private void do_NBJL_denglu() {
        final java.lang.String res = "OK";
        this.TCPDataSend(this._RemoteIP, this._RemotePort, res);
        int idx = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.TCP_Terms.size(); ++i) {
            if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemotePort == this._RemotePort && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_Type.equals("6")) {
                idx = i;
                break;
            }
        }
        if (idx <= -1) {
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            final java.lang.String sql = "select * from t_yb yb where 唯一码='" + this.WYM + "'";
            final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
            try {
                rs.next();
                this.YBH = rs.getString("仪表号").trim();
                this.TelNO = rs.getString("手机卡号").trim();
            }
            catch (java.lang.Exception e) {
                javagprssrv.JGS_Param.myLog.ShowLog("[ 系统未登记该仪表 ] = " + sql, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                return;
            }
            this.term.Term_Type = "6";
            this.term.Term_SerialNo = this.YBH;
            this.term.Term_TelNo = this.TelNO;
            this.term.Term_WYM = this.WYM;
            this.term.Term_RemoteIP = this._RemoteIP;
            this.term.Term_RemotePort = this._RemotePort;
            synchronized (javagprssrv.JGS_Param.TCP_Terms) {
                for (int j = javagprssrv.JGS_Param.TCP_Terms.size() - 1; j >= 0; --j) {
                    if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemotePort == this._RemotePort) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                    else if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_WYM.equals(this.WYM)) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                }
                javagprssrv.JGS_Param.TCP_Terms.add(this.term);
            }
        }
    }
    
    private void locateTask() {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    break;
                }
            }
        }
    }
    
    private void do_41() throws java.lang.Exception {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    if (this.JobIndex >= javagprssrv.JGS_Param.Tasks.elementAt(i).Task_Jobs.size()) {
                        javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = "2000-01-01 01:02:03";
                        break;
                    }
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_59() throws java.lang.Exception {
        this.AsynTelNo = this._Data.substring(1, 9);
        int idxTask = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 1) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).TaskAsynTelNO.equals(this.AsynTelNo)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP = this.recvData.RemoteIP;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port = java.lang.String.valueOf(this.recvData.RemotePort);
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State = 2;
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_nextJob() throws java.lang.Exception {
        java.lang.String job = this.task.Task_Jobs.elementAt(this.JobIndex).trim().toUpperCase();
        job += "                ";
        if (job.substring(0, 9).equals("GET DATE ")) {
            this.do_getDate(job);
        }
        if (job.substring(0, 10).equals("GET MONTH ")) {
            this.do_getMonth(job);
        }
    }
    
    private void do_getDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET DATE ")) {
            return;
        }
        java.lang.String cmdString = "";
        java.lang.String date = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4) + job.substring(9, 13);
        final int y = java.lang.Integer.valueOf(date.substring(0, 2));
        final int m = java.lang.Integer.valueOf(date.substring(2, 4));
        final int d = java.lang.Integer.valueOf(date.substring(4, 6));
        date = "" + (char)y + (char)m + (char)d;
        cmdString = "\u0006d\u0000\u0000A\t\f" + date + '\0';
        cmdString += this.NBJL_CRC(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_getMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET MONTH ")) {
            return;
        }
        java.lang.String cmdString = "";
        java.lang.String date = new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4) + job.substring(10, 12);
        final int y = java.lang.Integer.valueOf(date.substring(0, 2));
        final int m = java.lang.Integer.valueOf(date.substring(2, 4));
        date = "" + (char)y + (char)m;
        cmdString = "\u0006d\u0000\u0000A\n\u000b" + date + '\0';
        cmdString += this.NBJL_CRC(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void TCPDataSend(final java.lang.String RemoteIP, final int RemotePort, final java.lang.String strSend) {
        int idx = -1;
        for (int i = javagprssrv.JGS_Param.TCPServerSockets.size() - 1; i >= 0; --i) {
            if (((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemoteIP.equals(RemoteIP) && ((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemotePort == RemotePort) {
                idx = i;
                break;
            }
        }
        if (idx > -1) {
        	((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(idx)).SendData(strSend);
        }
    }
    
    public java.lang.String NBJL_CRC(final java.lang.String dat) {
        java.lang.String res = "";
        int sum = 0;
        for (int i = 0; i < dat.length(); ++i) {
            sum += dat.charAt(i);
        }
        sum %= 255;
        res = "" + (char)sum;
        return res;
    }
}
